<?
if(!defined('IN_IVOTE')) {
	exit('Access Denied');
}

function showDelete() {
	global $_SGLOBAL, $_FB;
	$deleteData = array(
		'sql'=>"",
		'pid'=>0
	);
 	if (isset($_GET['pid'])) {
		$deleteData['pid'] = intval($_GET['pid']);
	}
	$deleteData['sql'] = "DELETE FROM ".tname("storeSample")." WHERE pid=".$deleteData['pid'];
	$query = $_SGLOBAL['db']->query($deleteData['sql']);
	if ($_SGLOBAL['db']->affected_rows() != 1) {
		showmessage("wrong pid");
	} else {
		?>
		<table cellspacing="0">
			<tr class="noEntry"><td><h4>Succesfully deleted!</h4><div class="subheader">Click <a href="?action=home">here</a> to go back.</div></td></tr>
		</table>
		</div>
		</div>
		<?
	}
	
}


function showDefault() {
	global $_SGLOBAL, $_FB;
	$header = array(
		"title"=>"Administrator's Page",
		"hasButton"=>1
	);
	include_once template("admin");
?>

<div class="fbwhitebox" style="width: 738px;">  
<table cellspacing="0" width="60%">

<?

	global $_SGLOBAL;
	$sql = "SELECT pid, subject, name, posttime FROM ".tname('storeSample')." ORDER BY pid desc";
	$query = $_SGLOBAL['db']->query($sql);
	
	if ($_SGLOBAL['db']->num_rows($query) == 0) {
		echo '<tr class="noEntry"><td><h4>There is no sample questions yet.</h4><div class="subheader">Click <a href="?action=add">here</a> to create</div></td></tr>';
	} else {
		$i = 1;
		while ($arr = $_SGLOBAL['db']->fetch_array($query)) {
			
			echo '<tr><td><h4><a href="?action=edit&pid='.$arr['pid'].'">'.$arr['subject'].'</a></h4><div class="subheader">by '.$arr['name'].' on '.date('Y-m-d H:i:s',$arr['posttime']).'</div></td></tr>';
			
			$i++;
		}
	}

?>

</table> 
</div>

</div>

<?

}


function showSubmit($type) {

	global $_FB, $_USER, $_SGLOBAL;
	$options = array();
	$subject = "";
	$duration = 1;
	$sex = 0;
	$credit = 0;
	$pid = 0;
	if (isset($_POST['pid'])) {	
		$pid = $_POST['pid'];
	}	
	if (isset($_POST['subject'])) {	
		$subject = $_POST['subject'];
	}
	if (isset($_POST['duration'])) {
		$duration = trim($_POST['duration']);
		if (strcmp($duration,"3 days") == 0) {
			$duration = 259200; // 60*60*24*3
		} else if (strcmp($duration,"1 week") == 0) {
			$duration = 604800; // 60*60*24*7
		} else if (strcmp($duration,"2 week") == 0) {
			$duration = 1209600; // 60*60*24*14
		}
	}
	if (isset($_POST['options'])) {	
		$options = $_POST['options'];
		$options = preg_replace('/\r\n|\r/', "\n", $options);
		$options = explode("\n", $options);	
	}

	
	for ($i = 0; $i < count($options); $i++) {
			$optiops[$i] = trim($options[$i]);
			if (empty($options[$i])) {
				array_splice($options, $i, 1);
			}
	}
	$choice = count($options);
	$options = implode("\n", $options);
	$arr = array(
		"uid"=>$_FB['uid'],
		"name"=>$_USER['name'],
		"subject"=>$subject,
		"multiple"=> $choice>1 ? 1:0,
		"maxchoice"=>$choice,
		"sex"=>$sex,
		"credit"=>$credit,
		"duration"=>$duration,
		"options"=>$options,
		"posttime"=>$_SGLOBAL['timestamp']
	);
	
	if ($type == "add") {
		inserttable("storeSample", $arr);
	} else if ($type = "edit") {
		if ($pid) {
			updatetable('storeSample', $arr, array('pid'=>$pid));
		} else {
			showmessage("pid does not exist");
		}
	}
	?>
	<table cellspacing="0">
		<tr class="noEntry"><td><h4>Succesfully <?echo $type=="add" ? "added" : "edited"?>!</h4><div class="subheader">Click <a href="?action=home">here</a> to go back.</div></td></tr>
	</table>
	</div>
	</div>
	<?
}

function showAdd($type) {
	global $_SGLOBAL;
	?>

	<div class="addPage">
	<div class="dashboard_header">
		<div class="dh_links clearfix">

			<div class="dh_actions">
				<a href="?action=home">Back to admin home</a>
			</div>
		</div>
	<div class="dh_titlebar clearfix"><img src="http://static.ak.fbcdn.net/rsrc.php/z5YUV/hash/7abvozy3.png" class="icon" /><h2>Add New Sample Question</h2>


	</div>
	<div class="clear"></div>
	</div>
	<!--
		<div class="UISteps">
		   <div class="UIStep_Activated">
			  <div class="UIStep_Part UIStep_Back"></div>
			  <div class="UIStep_Part UIStep_Middle">
				<div class="UIStep_Content">
				  <div class="UIStep_Title">Step 1</div>
				  <div class="UIStep_Description">Create Event</div>
				 </div>
			   </div>
			  <div class="UIStep_Part UIStep_Point"></div>
			</div>
		   <div class="">
			  <div class="UIStep_Part UIStep_Back"></div>
			  <div class="UIStep_Part UIStep_Middle">
				<div class="UIStep_Content">
				  <div class="UIStep_Title">Step 2</div>
				  <div class="UIStep_Description">Add Details</div>
				 </div>
			   </div>
			  <div class="UIStep_Part UIStep_Point"></div>
			</div>
		   <div class="">
			  <div class="UIStep_Part UIStep_Back"></div>
			  <div class="UIStep_Part UIStep_Middle">
				<div class="UIStep_Content">
				  <div class="UIStep_Title">Step 3</div>
				  <div class="UIStep_Description">Invite Friends</div>
				 </div>
			   </div>
			  <div class="UIStep_Part UIStep_Point"></div>
			</div><div class="UIStep_Nub"></div>
		</div>
	-->
	<div class="fbgreybox" style="width: 738px;"> 

	<?
	if ($type == "add" || $type == "edit") {
		$display = array();
		$display['pid'] = $display['subject'] = $display['duration'] = $display['options'] ="";
		
		if ($type == "edit") {
			$sql = "SELECT pid, subject, duration, options FROM ".tname('storeSample')." where pid = ".intval($_GET['pid']);
			$query = $_SGLOBAL['db']->query($sql);
			if(!$display = $_SGLOBAL['db']->fetch_array($query)) {
				showmessage("find no entry");
			}
			//$display['options'] = nl2br($display['options']);
		}

	?>

	<form name="add" action="?action=<?=$type?>&submitting" method="POST">
	<input type="hidden" name="pid" value="<?=$display['pid']?>">
	<table cellspacing="0" border="0" style="width: 100%" class="editorkit">
	<tbody><tr class="width_setter"><th style="width: 100px;"></th><td></td></tr>
			<tr><th><label>Subject:</label></th><td class="editorkit_row"><input type="text" name="subject" value="<?=$display['subject']?>"></td><td class="right_padding"></td></tr>
			<!--<tr><th><label>Options:</label></th><td class="editorkit_row"><input type="text" name="options[]"></td><td class="right_padding"></td></tr>
			-->	
			<tr><th class="detached_label"><label>Duration:</label></th><td class="editorkit_row">
				<select name="duration">
					<option <?echo $display['duration'] == 259200 ? 'selected=""' : ''?> value="3 days">3 days</option>
					<option <?echo $display['duration'] == 604800 ? 'selected=""' : ''?> value="1 week">1 week</option>
					<option <?echo $display['duration'] == 1209600 ? 'selected=""' : ''?> value="2 weeks">2 weeks</option>
				</select>
				
			</td><td class="right_padding"></td></tr>
			
			<tr><th class="detached_label"><label>Options:</label></th><td class="editorkit_row"><textarea name="options" rows="8"><?=$display['options']?></textarea></td><td class="right_padding">Place one option per line</td></tr>
			<tr><th></th><td class="editorkit_buttonset">
				<input type="submit" value="<?=ucwords($type)?>" class="editorkit_button action">
				<? if ($type == "edit") {
					echo '<span class="cancel_link"><span>or</span><a href="?action=delete&pid='.$display['pid'].'">Delete</a></span>';
					}
				?>
				<span class="cancel_link"><span>or</span><a href="?action=home">Cancel</a></span>
			
			</td><td class="right_padding"></td></tr>
		</tbody>
	</table>
	</form>
	<script>
	function addOption() {
		var obj = document.getElementById("optionSpace2");
		//var newOption = document.createElement('fb:editor-text');
		//obj.appendChild(newOption);
		obj.setInnerFBML(newOption);
	}
	</script>
	<?
	}
}
?>